package fr.zadoka.zadokadna.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.util.Log;

public class CalendarUtil {

	public static String GregorianCalendarToString(GregorianCalendar date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault());
		return sdf.format(date.getTime());
		
	}
	
	public static GregorianCalendar StringToGregorianCalendar(String date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd", Locale.getDefault());
		Date convertedDate = new Date();
		try {
			convertedDate = sdf.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(convertedDate);
		return cal;
	}
	
	public static void LogDate(final String tag, final String message, final GregorianCalendar date)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
		String formatedDate = sdf.format(date.getTime());
		
		Log.i(tag, message + formatedDate);
	}
	
	public static boolean isSameDate(GregorianCalendar date1, GregorianCalendar date2) {
		// TODO Auto-generated method stub
		return (date1.get(GregorianCalendar.DAY_OF_MONTH) == date2.get(GregorianCalendar.DAY_OF_MONTH)) &&
			   (date1.get(GregorianCalendar.MONTH) == date2.get(GregorianCalendar.MONTH)) &&
			   (date1.get(GregorianCalendar.YEAR) == date2.get(GregorianCalendar.YEAR));	
	}
	
	public static GregorianCalendar millisecondsToGregorianCalendar(long time)
	{
		GregorianCalendar result = new GregorianCalendar();
		result.setTimeInMillis(time);
		return result;
	}
	
	public static int numberOfDaysBetween(GregorianCalendar cal1, GregorianCalendar cal2)
	{
		Date date1 = cal1.getTime();
		Date date2 = cal2.getTime();
		
		long startTime = date1.getTime();
		long endTime = date2.getTime();
		long diffTime = Math.abs(endTime - startTime);
		long diffDays = diffTime / (1000 * 60 * 60 * 24);
			
		return (int) diffDays;
	}
}
